
# -*- coding: utf-8 -*-
import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDockWidget, QAction, QFileDialog
import cv2
import numpy as np
import copy
from imageManager import *


class AdaptiveGui(QtWidgets.QWidget):
    update_signal = QtCore.pyqtSignal(QtGui.QImage)

    def __init__(self, parent=None):
        super(AdaptiveGui, self).__init__(parent)
        self.imgMat = None
        self.initUI()
        if imgmng.img is not None:
            self.imgMat = copy.deepcopy(imgmng.img)

    def convertQtImg(self, imgMat):
        if len(self.imgMat.shape) == 3:
            img = cv2.cvtColor(imgMat, cv2.COLOR_BGR2RGB)
        else:
            img = cv2.cvtColor(imgMat, cv2.COLOR_GRAY2RGB)
        h, w = img.shape[:2]
        h = (h + 3) // 4
        w = (w + 3) // 4
        print(h, w)
        shrink = cv2.resize(img, (4 * w, 4 * h), interpolation=cv2.INTER_AREA)
        QtImg = QtGui.QImage(shrink.data,
                             shrink.shape[1],
                             shrink.shape[0],
                             QtGui.QImage.Format_RGB888)

        return QtImg

    def initUI(self):
        print("ddd")
        self.histLabel = QtWidgets.QLabel(self)
        self.histLabel.setText("ddda")
        # self.histLabel.setFixedSize(200, 200)

        self.thrsSplider = QtWidgets.QSlider(self)
        self.thrsSplider.setRange(1, 255)
        self.thrsSplider.setSingleStep(1)
        self.thrsSplider.setOrientation(1)
        self.thrsSplider.setTickPosition(QtWidgets.QSlider.TicksBelow)
        self.thrsSplider.setTickInterval(5)

        okbtn = QtWidgets.QPushButton(self)
        okbtn.setText("apply")

        vlayout= QtWidgets.QVBoxLayout(self)
        vlayout.addWidget(self.histLabel)
        vlayout.addWidget(self.thrsSplider)
        vlayout.addWidget(okbtn)
        okbtn.clicked.connect(self.okbtn_clicked)
        self.thrsSplider.valueChanged.connect(self.changeThresValue)

    def changeThresValue(self):
        if self.imgMat is None:
            return
        print(self.thrsSplider.value())
        value = self.thrsSplider.value()
        if len(self.imgMat.shape) == 3:
            self.imgMat = cv2.cvtColor(self.imgMat, cv2.COLOR_RGB2GRAY)
        # self.imgMat = cv2.threshold(self.imgMat, value, 255)
        ret, thresh = cv2.threshold(self.imgMat, value, 255, 0)
        # cv2.imshow("dd", thresh)
        # cv2.waitKey(0)
        if self.imgMat is not None:
            qimg = self.convertQtImg(thresh)
            self.update_signal.emit(qimg)

    def okbtn_clicked(self):
        if self.imgMat is None:
            return
        # if len(self.imgMat.shape) == 3:
        #     b, g, r = cv2.split(self.imgMat)
        #     hisimg = calcAndDrawHist(g, [255, 0, 0])
        #     h = (int(self.histLabel.width()) + 3) // 4
        #     w = (int(self.histLabel.height()) + 3) // 4
        #     size = (4 * w, 4 * h)
        #     shrink = cv2.resize(hisimg, size, interpolation=cv2.INTER_AREA)
        #     # cv2.imshow('img', shrink)
        #     shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
        #     QtImg = QtGui.QImage(shrink.data,
        #                               shrink.shape[1],
        #                               shrink.shape[0],
        #                               QtGui.QImage.Format_RGB888)
        #     self.histLabel.setPixmap(QtGui.QPixmap.fromImage(QtImg))
        # else:
        #     hisimg = calcAndDrawHist(self.imgMat, [255, 0, 0])
        #
        #     h = (int(self.histLabel.width()) + 3) // 4
        #     w = (int(self.histLabel.height()) + 3) // 4
        #     size = (4 * w, 4 * h)
        #     shrink = cv2.resize(hisimg, size, interpolation=cv2.INTER_AREA)
        #     # cv2.imshow('img', shrink)
        #     shrink = cv2.cvtColor(shrink, cv2.COLOR_BGR2RGB)
        #     QtImg = QtGui.QImage(shrink.data,
        #                          shrink.shape[1],
        #                          shrink.shape[0],
        #                          QtGui.QImage.Format_RGB888)
        #     self.histLabel.setPixmap(QtGui.QPixmap.fromImage(QtImg))

